package com.xgpro.pc.manage.service.impl;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.xgpro.domain.studycentre.entity.UserCourse;
import com.xgpro.domain.talentintroduction.dto.UserMessage;
import com.xgpro.mapper.studycentre.UserCourseMapper;
import com.xgpro.mapper.talentintroduction.UserMapper;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xgpro.common.service.BaseService;
import com.xgpro.common.utils.ReadPropertiesUtil;
import com.xgpro.domain.studycentre.entity.Course;
import com.xgpro.domain.studycentre.vo.FirstTea;
import com.xgpro.mapper.studycentre.CourseMapper;
import com.xgpro.mapper.studycentre.TeaMapper;
import com.xgpro.pc.manage.service.FirstPageService;

import tk.mybatis.mapper.entity.Example;

/**
 * @author tx
 * @version 1.0
 * @date 2019/11/28 21:49
 */
@Service
@Transactional(readOnly = false)
public class FirstPageServiceImpl extends BaseService implements FirstPageService {

	@Resource
	private TeaMapper teaMapper;

	@Resource
	private CourseMapper courseMapper;

	/**
	 * 首页导师数
	 * 
	 * @return
	 */
	@Override
	public Integer teacherNums() {
		return teaMapper.selectAll().size();
	}

	/**
	 * 首页课程数
	 * 
	 * @return
	 */
	@Override
	public Integer courseNums() {
		Example example = new Example(Course.class);
		example.createCriteria().andEqualTo("status", 1);
		return courseMapper.selectCountByExample(example);
	}

	/**
	 * 课程时长排行
	 * 
	 * @return
	 */
	@Override
	public List<FirstTea> courseTimeSort() {
		List<FirstTea> firstTeas = new ArrayList<>();
		List<Course> courses = courseMapper.selectAll();
		if (courses.size() > 0) {
			courses = courses.stream().filter(x -> x.getTeaId() != null && x.getCourseType() == 0)
					.collect(Collectors.toList());
			Set<Integer> set = courses.stream().map(x -> x.getTeaId()).collect(Collectors.toSet());
			FirstTea firstTea = null;
			for (Integer userId : set) {
				Example example = new Example(Course.class);
				example.createCriteria().andEqualTo("teaId", userId);
				List<Course> courseList = courseMapper.selectByExample(example);
				String teaUsername = courseList.get(0).getTeaUsername();
				Integer courseTime = 0;
				for (Course course : courseList) {
					courseTime += course.getCourseTime();
				}
				firstTea = new FirstTea();
				firstTea.setCourseTime(courseTime);
				firstTea.setTeaName(teaUsername);
				firstTeas.add(firstTea);
			}
			Collections.sort(firstTeas, (p1, p2) -> p2.getCourseTime().compareTo(p1.getCourseTime()));
		}
		Integer teaCount = Integer.valueOf(ReadPropertiesUtil.readUrl("tea_count"));
		if (firstTeas.size() > teaCount) {
			List<FirstTea> firstTeaList = new ArrayList<FirstTea>();
			Integer count = 0;
			for (FirstTea firstTea : firstTeas) {
				firstTeaList.add(firstTea);
				count++;
				if (count.equals(teaCount)) {
					break;
				}
			}
			firstTeas = firstTeaList;
		}
		return firstTeas;
	}

	/**
	 * 上课人数排行
	 */
	@Override
	public List<Map> courseStudentNum() {
		List<Map> list = new ArrayList<Map>();
		Map map = null;
		Example example = new Example(Course.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("status", 1);
		criteria.andEqualTo("teaStatus", 1);
		example.setOrderByClause("student_num DESC");
		PageHelper.startPage(1, 10);
		Page<Course> pages = (Page<Course>) courseMapper.selectByExample(example);
		long num = pages.getTotal();
		if (num > 0) {
			for (Course course : pages.getResult()) {
				map = new HashedMap();
				map.put("courseName", course.getTitle());
				map.put("teaName",course.getTeaUsername());
				map.put("courseNum", course.getStudentNum());
				list.add(map);
			}
		}
		return list;
	}


	/**
	 * 课程种类分布
	 * @return
	 */
	@Override
	public List<Map> courseKind() {
		List<Map> list = new ArrayList<Map>();
		Example example = new Example(Course.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("status",1);
		criteria.andEqualTo("courseType",0);
		int nCount = courseMapper.selectCountByExample(example);
		Map map1 = new HashMap();
		map1.put("name","内部课程");
		map1.put("num",nCount);
		list.add(map1);
		example = new Example(Course.class);
		Example.Criteria criteria2 = example.createCriteria();
		criteria2.andEqualTo("status",1);
		criteria2.andEqualTo("courseType",1);
		int wCount = courseMapper.selectCountByExample(example);
		Map map2=new HashMap();
		map2.put("name","外部课程");
		map2.put("num",wCount);
		list.add(map2);
		example = new Example(Course.class);
		Example.Criteria criteria3 = example.createCriteria();
		criteria3.andEqualTo("status",1);
		criteria3.andEqualTo("courseType",2);
		int sCount = courseMapper.selectCountByExample(example);
		Map map3=new HashMap();
		map3.put("name","微课程");
		map3.put("num",sCount);
		list.add(map3);
		return list;
	}

	@Resource
	private UserCourseMapper userCourseMapper;

	@Resource
	private UserMapper userMapper;
	/**
	 * 折线图: 学员公司级,部门级听课时长排行
	 * @return
	 */
	@Override
	public Map courseDeptSort() {
		Map map = new HashMap();
		Example example1 = new Example(UserCourse.class);
		example1.createCriteria().andEqualTo("status",1);
		List<UserCourse> userCourses = userCourseMapper.selectByExample(example1);
		if(userCourses!=null&&userCourses.size()>0){
			List<Integer> studentIds = userCourses.stream().map(x -> x.getStudentId()).collect(Collectors.toList());
			Set<Integer> stuSet=new HashSet<Integer>();
			for (Integer studentId : studentIds) {
				stuSet.add(studentId);
			}
			List<Integer> courseIds = userCourses.stream().map(x -> x.getCourseId()).collect(Collectors.toList());
			Set<Integer> set=new HashSet<Integer>();
			for (Integer courseId : courseIds) {
				set.add(courseId);
			}
			Example example = new Example(Course.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andIn("id",set);
			//criteria.andNotEqualTo("courseDept",null);
			List<Course> courses = courseMapper.selectByExample(example);
			List<Course> nCourse = new ArrayList<Course>();
			for (Course cours : courses) {
				if(cours.getCourseDept()!=null&&cours.getCourseDept().equals(0)){
					nCourse.add(cours);
				}
			}
			List<Course> wCourse = new ArrayList<Course>();
			for (Course cours : courses) {
				if(cours.getCourseDept()!=null&&cours.getCourseDept().equals(1)){
					wCourse.add(cours);
				}
			}
//			List<Course> nCourse = courses.stream().filter(x -> x.getCourseDept()==0).collect(Collectors.toList());
//			List<Course> wCourse = courses.stream().filter(x -> x.getCourseDept()==1).collect(Collectors.toList());
			List<Map> nMaps = new ArrayList<Map>();
			Map entity=null;
			ArrayList<Integer> ids = new ArrayList<>();
			for (Integer o : stuSet) {
					ids.add(o);
			}
			List<UserMessage> userMessages = userMapper.selectByList(ids);
			for (Integer integer : stuSet) {
				entity=new HashMap();
				int count=0;
				List<UserCourse> collect = userCourses.stream().filter(x -> x.getStudentId().equals(integer)).collect(Collectors.toList());
				if(collect!=null&&collect.size()>0){
					List<Integer> courseIdList = collect.stream().map(x -> x.getCourseId()).collect(Collectors.toList());
					for (Integer integer1 : courseIdList) {
						for (Course course : nCourse) {
							if(course.getId().equals(integer1)){
									count+=course.getCourseTime();
							}
						}
					}
				}
				List<UserMessage> collect1 = userMessages.stream().filter(x -> x.getId().equals(integer)).collect(Collectors.toList());
				if(collect1!=null&&collect1.size()>0){
					entity.put("name",collect1.get(0).getUsername());
					entity.put("time",count);
					nMaps.add(entity);
				}


			}
			List<Map> wMaps = new ArrayList<Map>();
			for (Integer integer : stuSet) {
				entity=new HashMap();
				int count=0;
				List<UserCourse> collect = userCourses.stream().filter(x -> x.getStudentId().equals(integer)).collect(Collectors.toList());
				if(collect!=null&&collect.size()>0){
					List<Integer> courseIdList = collect.stream().map(x -> x.getCourseId()).collect(Collectors.toList());
					for (Integer integer1 : courseIdList) {
						for (Course course : wCourse) {
							if(course.getId().equals(integer1)){
								count+=course.getCourseTime();
							}
						}
					}
				}
				List<UserMessage> collect1 = userMessages.stream().filter(x -> x.getId().equals(integer)).collect(Collectors.toList());
				if(collect1!=null&&collect1.size()>0){
					entity.put("name",collect1.get(0).getUsername());
					entity.put("time",count);
					wMaps.add(entity);
				}
			}
			Collections.sort(nMaps, new Comparator<Map>() {
				@Override
				public int compare(Map o1, Map o2) {
					if (((Integer)o1.get("time")) < ((Integer) o2.get("time"))) {
						return 1;
					}
					if (((Integer)o1.get("time")) .equals ((Integer) o2.get("time"))) {
						return 0;
					}
					return -1;
				}
			});
			Collections.sort(wMaps, new Comparator<Map>() {
				@Override
				public int compare(Map o1, Map o2) {
					if (((Integer)o1.get("time")) < ((Integer) o2.get("time"))) {
						return 1;
					}
					if (((Integer)o1.get("time")) .equals ((Integer) o2.get("time"))) {
						return 0;
					}
					return -1;
				}
			});
			map.put("innerCourse",nMaps);
			map.put("outterCourse",wMaps);
		}
		return map;
	}
}
